#include "common.hpp"
#include <iostream>
#include<cstdlib>
#include<sys/types.h>
#include<sys/socket.h>
#include"inetaddr.hpp"
#include<unistd.h>

int main(int argc,char** argv)
{
    if(argc != 3)
    {
        std::cerr<<"Usage:" << argv[0] << " serverip serverport" << std::endl;
        exit(static_cast<int>(ExitCode::USAGE_ERR));
    }
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    if(sockfd == -1)
    {
        std::cerr << "socke fail" << std::endl;
        exit(static_cast<int>(ExitCode::SOCKET_ERR));
    }
    InetAddr server(argv[1],std::stoi(argv[2]));
    int n = connect(sockfd,(struct sockaddr*)&server.GetAddr(),server.Getlen());
    if(n  == -1)
    {
        std::cerr << "connect fail" << std::endl;
        exit(static_cast<int>(ExitCode::CONNECT_ERR));
    }
    while(true)
    {
        std::string input;
        std::cout << "please input:";
        std::getline(std::cin,input);
        write(sockfd,input.c_str(),input.size());
        char buffer[1024];
        int n = read(sockfd,buffer,sizeof(buffer) - 1);
        buffer[n] = 0;
        std::cout << buffer << std::endl;
    }
     
    return 0;
}